package com.sam.im.heiqun.uis.activities;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.sam.im.heiqun.R;
import com.sam.im.heiqun.constant.Constant;
import com.sam.im.heiqun.entities.ImFriendEntivity;
import com.sam.im.heiqun.entities.UserEntivity;
import com.sam.im.heiqun.entities.ValidateEntivity;
import com.sam.im.heiqun.nets.PGService;
import com.sam.im.heiqun.uis.adapters.BlackFriendAdpter;
import com.sam.im.heiqun.utils.ToolsUtils;
import com.yuyh.library.nets.callbacks.AbsAPICallback;
import com.yuyh.library.nets.exceptions.ApiException;
import com.yuyh.library.uis.activitys.BaseSwipeBackActivity;
import com.yuyh.library.utils.DividerItemDecorationUtils;
import com.yuyh.library.utils.toast.ToastUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by wangguodong on 2017/1/11.
 * 黑名单列表
 */
public class BlackFriendActivity extends BaseSwipeBackActivity implements BlackFriendAdpter.FriendListClickListener,BlackFriendAdpter.BlackListClickListener{

    @BindView(R.id.list_friend)
    RecyclerView list_friend;
    @BindView(R.id.is_not_black_friend)
    TextView isNotBlackFriend;
    private BlackFriendAdpter mBlackFriendAdpter ;
    private List<ImFriendEntivity> imFriendEntivities ;
    PGService mPgService;
    private String uid = "" ;
    private String TAG = BlackFriendActivity.class.getSimpleName();

    @Override
    protected String getTitleText() {
        return getString(R.string.black_title);
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {

        mPgService = PGService.getInstance();
        UserEntivity userEntivity = ToolsUtils.getUser();
        imFriendEntivities = ImFriendEntivity.find(ImFriendEntivity.class, "currentid=? and is_black=?", userEntivity.getId() + "", Constant.IS_BLACK);

        if (imFriendEntivities.size()==0){
            isNotBlackFriend.setVisibility(View.VISIBLE);
            list_friend.setVisibility(View.GONE);
        }else{
            isNotBlackFriend.setVisibility(View.GONE);
            list_friend.setVisibility(View.VISIBLE);
        }

        list_friend.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
        mBlackFriendAdpter = new BlackFriendAdpter(getApplicationContext(),imFriendEntivities);
        mBlackFriendAdpter.setItemClickListener(this);
        mBlackFriendAdpter.setItemRmClickListener(this);
        list_friend.setAdapter(mBlackFriendAdpter);
        list_friend.addItemDecoration(new DividerItemDecorationUtils(getApplicationContext(), LinearLayoutManager.HORIZONTAL, 1, getResources().getColor(R.color.black_divider)));
    }

    @Override
    public int getContentViewId() {
        return R.layout.activity_black_friend;
    }

    @Override
    public void onAgreeNewFriendClick(View view, int position) {
        Bundle bundle = new Bundle();
        bundle.putLong("id", imFriendEntivities.get(position).getId());
        bundle.putInt("type",1);
        startActivity(FriendDetailActivity.class,bundle);
    }
    @Override
    public void onBlackListClickListener(View view, int position) {
        uid = ""+imFriendEntivities.get(position).getId();
        removeBlack(uid);
    }

    /**
     * 移除黑名单
     * @param uId
     */
    public void removeBlack(final String uId){
        mPgService.removeBlack(uId, ToolsUtils.getMyUserId()).subscribe(new AbsAPICallback<ValidateEntivity>() {
            @Override
            protected void onResultError(ApiException ex) {
                new ToastUtils().showSingleToast(ex.getDisplayMessage());
            }
            @Override
            public void onNext(ValidateEntivity data) {
                new ToastUtils().showSingleToast(data.getInfo());
                List<ImFriendEntivity> mGroupSNew = new ArrayList<>();
//                    Log.i(TAG, "移除黑名单: mGroupSNew.size()="+mGroupSNew.size());
                ImFriendEntivity imFriendEntivity = ImFriendEntivity.findById(ImFriendEntivity.class,Long.parseLong(uId));
                imFriendEntivity.setIsBlack(Constant.IS_NOT_BLACK);
                imFriendEntivity.save();
                EventBus.getDefault().post(Constant.BLACK_BROADCAST_ADD_GROUP_FOUND_REFRESH);
                scrollToFinishActivity();
            }
        });
    }

}
